package org.uugu.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.uugu.common.gui.DrawableUtils;
import org.uugu.common.gui.Version;

import com.iflytek.speech.SpeechError;
import com.iflytek.speech.SynthesizerPlayer;
import com.iflytek.speech.SynthesizerPlayerListener;

public class SynthesizeView extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	//合成的文本内容
	private final static String TEXT_CONTENT = "研究";
	
	private JButton jbtLeft;
	private JButton jbtRight;	
	private JButton jbtHome;
	private JButton jbtSetting;
	
	private JTextArea resultArea;
	
	/**
	 * 初始化按钮.
	 * 初始化按钮图片背景、大小、鼠标点击事件
	 */
	public SynthesizeView()
	{
		ImageIcon imgLeft = new ImageIcon("res/button_left.png");
		jbtLeft = DrawableUtils.createImageButton("朗读", imgLeft, "center");		
		jbtLeft.setBounds(0, 320, imgLeft.getIconWidth(), imgLeft.getIconHeight()*4/5);
		DrawableUtils.setMouseListener(jbtLeft, "res/button_left");
		
		ImageIcon imgRight = new ImageIcon("res/button_Right.png");
		jbtRight = DrawableUtils.createImageButton("取消", imgRight, "center");		
		jbtRight.setBounds(330, 320, imgRight.getIconWidth(), imgRight.getIconHeight()*4/5);
		DrawableUtils.setMouseListener(jbtRight, "res/button_Right");	
		
		ImageIcon imgHome =  new ImageIcon("res/home.png");
		jbtHome = DrawableUtils.createImageButton("", imgHome, null);
		jbtHome.setBounds(20, 20, imgHome.getIconWidth(), imgHome.getIconHeight());
		DrawableUtils.setMouseListener(jbtHome, "res/home");
		
		ImageIcon imgSetting =  new ImageIcon("res/setting.png");
		jbtSetting = DrawableUtils.createImageButton("", imgSetting, null);
		jbtSetting.setBounds(530, 20, imgSetting.getIconWidth(), imgSetting.getIconHeight());
		DrawableUtils.setMouseListener(jbtSetting, "res/setting");
		
		resultArea = new JTextArea("");
		resultArea.setBounds(40, 100, 500, 200);
		resultArea.setOpaque(true);
		resultArea.setEditable(true);
		resultArea.setLineWrap(true);
		resultArea.setForeground(Color.BLACK);
		resultArea.setDragEnabled(true);

		Font font=new Font("隶书", Font.BOLD, 30);
		resultArea.setFont(font);
		resultArea.setText(TEXT_CONTENT);
		JPanel jp = new JPanel();
		jp.add(new JScrollPane(resultArea));
		jp.setVisible(true);
//		JScrollPane scroll = new JScrollPane(resultArea); 
//		//分别设置水平和垂直滚动条自动出现 
//		scroll.setHorizontalScrollBarPolicy( 
//		JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
//		scroll.setVerticalScrollBarPolicy( 
//		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
//		resultArea.setLayout(new GridLayout(1,1));//加上这句
		setOpaque(false);
		setLayout(null);	
		add(jbtLeft);
		add(jbtRight);
		add(resultArea);
		add(jbtHome);
		add(jbtSetting);

		
		if(SynthesizerPlayer.getSynthesizerPlayer() == null)
			SynthesizerPlayer.createSynthesizerPlayer("appid=" + Version.getAppid());
		
		jbtLeft.addActionListener(this);
		jbtRight.addActionListener(this);
		jbtHome.addActionListener(this);
	}
	
	/**
	 * 按钮监听器实现
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		SynthesizerPlayer synthesizer = SynthesizerPlayer.getSynthesizerPlayer();
		if(e.getSource() == jbtLeft) {
			//设置发音人为小宇
			synthesizer.setVoiceName("xiaoyu");
			//设置朗读速度为50
			synthesizer.setSpeed(50);
			//合成文本为TEXT_CONTENT的句子，设置监听器为mSynListener
			synthesizer.playText(resultArea.getText().trim(), null, mSynListener);
		} 
		else if (e.getSource() == jbtRight) {
			synthesizer.cancel();
		}
		else if (e.getSource() == jbtHome) {
			JFrame frame = MainView.getFrame();
			frame.getContentPane().remove(this);
			JPanel panel = ((MainView) frame).getMainJpanel();
			frame.getContentPane().add(panel);
			frame.getContentPane().validate();
			frame.getContentPane().repaint(); 
		}
		
	}
	
	private SynthesizerPlayerListener mSynListener = new SynthesizerPlayerListener()
	{

		@Override
		public void onEnd(SpeechError error) {						
		}

		@Override
		public void onBufferPercent(int percent, int beginPos, int endPos) {
			
		}

		@Override
		public void onPlayBegin() {
			
		}

		@Override
		public void onPlayPaused() {
			
		}

		@Override
		public void onPlayPercent(int percent, int beginPos, int endPos) {			
		}

		@Override
		public void onPlayResumed() {
			
		}
	};
}
